package rexsee.cartoon.gif;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.style.CanvasSheet;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

public class RexseeGifDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "GifDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeGifDialog(childBrowser);
	}

	public static final String EVENT_ONGIFDIALOGDISMISSED = "onGifDialogDismissed";

	private final Context mContext;
	private final Browser mBrowser;
	private final HashMap<String, GifDialog> mMap;
	private boolean syncTag = false;

	public RexseeGifDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
		mMap = new HashMap<String, GifDialog>();
		mBrowser.eventList.add(EVENT_ONGIFDIALOGDISMISSED);
	}

	//JavaScript Interface 
	public void show(final String id, final String url, final String dialogStyle, final String gifStyle) {
		if (mMap.containsKey(id)) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					final GifDialog gifDialog = new GifDialog(mBrowser, dialogStyle);
					mMap.put(id, gifDialog);
					gifDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							mMap.remove(id);
							gifDialog.destroy();
							mBrowser.eventList.run(EVENT_ONGIFDIALOGDISMISSED, new String[]{id});
						}
					});
					gifDialog.start();
					gifDialog.setGif(url, new CanvasSheet(mContext, gifStyle));
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}

	public void dismiss(String id) {
		if (!mMap.containsKey(id)) return;
		GifDialog dialog = mMap.get(id);
		if (dialog != null) dialog.dismiss();
		mMap.remove(id);
	}
	public void dismissAll() {
		Set<String> keys = mMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			GifDialog dialog = mMap.get(key);
			if (dialog != null) dialog.dismiss();
		}
		mMap.clear();
	}
	public void setStyle(final String id, final String style) {
		if (!mMap.containsKey(id)) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					mMap.get(id).setStyle(style);
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public String getStyle(String id, String name) {
		return (!mMap.containsKey(id)) ? "" : mMap.get(id).getStyle(name);
	}
	public boolean exists(String id) {
		return mMap.containsKey(id);
	}
	public int size() {
		return mMap.size();
	}
	public String getIds() {
		String rtn = "";
		Set<String> ids = mMap.keySet();
		Iterator<String> iterator = ids.iterator();
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + iterator.next() + "\"";
		}
		return "[" + rtn + "]";
	}

	public void loadGif(String id, final String url, final String gifStyle) {
		if (!mMap.containsKey(id)) return;
		final GifDialog dialog = mMap.get(id);
		if (dialog == null) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				dialog.setGif(url, new CanvasSheet(mContext, gifStyle));
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}
	public void clearGif(String id) {
		if (!mMap.containsKey(id)) return;
		final GifDialog dialog = mMap.get(id);
		if (dialog == null) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				dialog.setGif(null, null);
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}
	public void setClickCallback(String id, final String url) {
		if (!mMap.containsKey(id)) return;
		final GifDialog dialog = mMap.get(id);
		if (dialog == null) return;
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
		dialog.setClickCallback(runnable);
	}
	public void clearClickCallback(String id) {
		if (!mMap.containsKey(id)) return;
		final GifDialog dialog = mMap.get(id);
		if (dialog == null) return;
		dialog.setClickCallback(null);
	}

}
